package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.shop.bo.BalanceHistoryBO;
import com.junzhiit.nengji.shop.dao.BalanceHistoryDAO;
import com.junzhiit.nengji.shop.model.BalanceHistory;
import com.junzhiit.nengji.shop.model.enums.BalanceHistoryStatus;
import com.junzhiit.nengji.shop.model.enums.Is;
import com.junzhiit.nengji.shop.model.enums.OperationType;

@Repository
public class BalanceHistoryDAOImpl extends AbstractBaseDAO<BalanceHistory> implements BalanceHistoryDAO{

	@Override
	public List<BalanceHistoryBO> findAll(int pageNumber, int maxPageRow, BalanceHistoryBO balanceHistoryBO) {
		StringBuffer sql = new StringBuffer();
		sql.append("select bh.*,sc.balance,u.nick_name 'name',u.mobile_phone,c.name 'coupons_name'");
		sql.append(" from balance_history bh");
		sql.append(" inner join shop_client sc on sc.id = bh.client_id");
		sql.append(" inner join user u on sc.user_id = u.id");
		sql.append(" left join coupons c on bh.coupons_id = c.id");
		sql.append(" where bh.is_delete = ? and bh.cs_id = ?");
		StatementParameter param = new StatementParameter();
		param.setInt(Is.NO.toIntValue());
		param.setString(balanceHistoryBO.getCsId());
		
		if(balanceHistoryBO.getOperation() == OperationType.ADD.toIntValue()){
			sql.append(" and bh.operation = ? ");
			param.setInt(OperationType.ADD.toIntValue());
		}
		if(balanceHistoryBO.getOperation() == OperationType.PAY.toIntValue()){
			sql.append(" and bh.operation = ? ");
			param.setInt(OperationType.PAY.toIntValue());
		}
		if(StringUtils.isNotBlank(balanceHistoryBO.getTransactionNumber())){
			sql.append(" and bh.transaction_number like ?");
			param.setString("%"+balanceHistoryBO.getTransactionNumber()+"%");
		}
		if(StringUtils.isNotBlank(balanceHistoryBO.getMobilePhone())){
			sql.append(" and u.mobile_phone like ?");
			param.setString("%"+balanceHistoryBO.getMobilePhone()+"%");
		}
		if(balanceHistoryBO.getStatus()!=null){
			sql.append(" and bh.status = ?");
			param.setInt(balanceHistoryBO.getStatus());
		}
		if(StringUtils.isNotBlank(balanceHistoryBO.getStartDate())){
			sql.append(" and bh.created_time >= ?");
			param.setDate(DateUtil.getDateStart(DateUtil.parseDate(balanceHistoryBO.getStartDate())));
		}
		if(StringUtils.isNotBlank(balanceHistoryBO.getEndDate())){
			sql.append(" and bh.created_time <= ?");
			param.setDate(DateUtil.getDateEnd(DateUtil.parseDate(balanceHistoryBO.getEndDate())));
		}
		
		return queryForPagingList(sql.toString(), BalanceHistoryBO.class, param, pageNumber, maxPageRow);
	}

	@Override
	public int getTotalCount(BalanceHistoryBO balanceHistoryBO) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(bh.id) from balance_history bh");
		sql.append(" inner join shop_client sc on sc.id = bh.client_id");
		sql.append(" inner join user u on sc.user_id = u.id");
		sql.append(" left join coupons c on bh.coupons_id = c.id");
		sql.append(" where bh.is_delete = ? and bh.cs_id = ?");
		StatementParameter param = new StatementParameter();
		param.setInt(Is.NO.toIntValue());
		param.setString(balanceHistoryBO.getCsId());
		
		if(balanceHistoryBO.getOperation() == OperationType.ADD.toIntValue()){
			sql.append(" and bh.operation = ? ");
			param.setInt(OperationType.ADD.toIntValue());
		}
		if(balanceHistoryBO.getOperation() == OperationType.PAY.toIntValue()){
			sql.append(" and bh.operation = ? ");
			param.setInt(OperationType.PAY.toIntValue());
		}
		if(StringUtils.isNotBlank(balanceHistoryBO.getTransactionNumber())){
			sql.append(" and bh.transaction_number like ?");
			param.setString("%"+balanceHistoryBO.getTransactionNumber()+"%");
		}
		if(StringUtils.isNotBlank(balanceHistoryBO.getMobilePhone())){
			sql.append(" and u.mobile_phone like ?");
			param.setString("%"+balanceHistoryBO.getMobilePhone()+"%");
		}
		if(balanceHistoryBO.getStatus()!=null){
			sql.append(" and bh.status = ?");
			param.setInt(balanceHistoryBO.getStatus());
		}
		if(StringUtils.isNotBlank(balanceHistoryBO.getStartDate())){
			sql.append(" and bh.created_time >= ?");
			param.setDate(DateUtil.getDateStart(DateUtil.parseDate(balanceHistoryBO.getStartDate())));
		}
		if(StringUtils.isNotBlank(balanceHistoryBO.getEndDate())){
			sql.append(" and bh.created_time <= ?");
			param.setDate(DateUtil.getDateEnd(DateUtil.parseDate(balanceHistoryBO.getEndDate())));
		}
		
		return queryForInt(sql.toString(),param);
	}

	@Override
	public BalanceHistoryBO findHeaderData(String csId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select sum(bh.balance_change) 'total_money',count(distinct bh.client_id) 'person',");
		sql.append("count(bh.id) 'number',sum(sc.balance) 'money' from balance_history bh");
		sql.append(" inner join shop_client sc on sc.id = bh.client_id");
		sql.append(" where bh.is_delete = ? and bh.cs_id = ? and bh.operation = ? and bh.status = ?");
		StatementParameter param = new StatementParameter();
		param.setInt(Is.NO.toIntValue());
		param.setString(csId);
		param.setInt(OperationType.ADD.toIntValue());
		param.setInt(Is.YES.toIntValue());
		
		return query(sql.toString(), BalanceHistoryBO.class, param);
	}

	@Override
	public BalanceHistory getByOrderNumber(String orderNumber) {
		String sql = " select * from balance_history where order_number = ? and status = ? and is_delete = ? ";
		StatementParameter param = new StatementParameter();
		param.setString(orderNumber);
		param.setInt(BalanceHistoryStatus.UNPAID.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		return query(sql.toString(), BalanceHistory.class, param);
	}

}
